import requests
import json

from BD2WGS import BD2WGS

AK = "S21GCfhh1vfSRZM89ppF0HYs7oAhQu99"
def postion2Location(data):
    try:
        address = "北京市" + data
        url = f"https://api.map.baidu.com/geocoding/v3/?address={address}&output=json&ak={AK}&callback=showLocation"
        res = requests.get(url)
        content = res.text.replace("showLocation&&showLocation(", "").replace(")", "")
        jd = json.loads(content)
        lng = jd['result']['location']['lng']
        lat = jd['result']['location']['lat']
        return BD2WGS(lng, lat)
    except:
        return 0, 0